package com.bridgeintelligent.tag.utils;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;

public class PasswordHelper {

    public static final String SALT = "Z20190926Z";

    /**
     * 密码编码
     *
     * @param original
     * @return
     */
    public static String encode(String original) {
        return DigestUtils.md5Hex(SALT + original);
    }




    /**
     * 校验密码
     * 1、长度不小于8位
     * 2、必须以字母开头
     * 3、必须包含特殊字符
     * 4、必须包含数字
     *
     * @param pwd
     * @return
     */
    public static Map<String, String> validPwd(String pwd) {
        Map<String, String> map = new HashMap<String, String>();
        if (StringUtils.isEmpty(pwd)) {
            map.put("F", "false");
            map.put("M", "密码不能为空");
            return map;
        }
        if (pwd.length() < 8) {
            map.put("F", "false");
            map.put("M", "密码不能少于8位");
            return map;
        }
        boolean matches2 = pwd.matches("^[a-zA-z](.*)");
        if (!matches2) {
            map.put("F", "false");
            map.put("M", "密码首位必须为字母");
            return map;
        }
        boolean matches1 = pwd.matches("(.*)[-`=\\\\\\[\\];',./~!@#$%^&*()_+|{}:\"<>?]+(.*)");
        if (!matches1) {
            map.put("F", "false");
            map.put("M", "密码必须含有特殊字符");
            return map;
        }
        boolean matches = pwd.matches("(.*)\\d+(.*)");
        if (!matches) {
            map.put("F", "false");
            map.put("M", "密码必须含有数字");
            return map;
        }

        map.put("F", "true");
        map.put("M", "密码合法");
        return map;
    }

    public static boolean checkSpecial(String param){
        if (StringUtils.isEmpty(param)){
            return false;
        }
        //排除掉 - — _ () （）情况
        param = param.replace("-","")
                .replace("_","")
                .replace("(","")
                .replace(")","")
                .replace("（","")
                .replace("）","");
        boolean matches = param.matches("(.*)[-`=\\\\\\[\\];',./~!@#$%^&*()_+|{}:\"<>?]+(.*)");
        if (matches){
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        String str = "11111";
        System.out.println(PasswordHelper.checkSpecial(str));
    }

}
